<?php

/**
 * This is the model class for table "order_attribute".
 *
 * The followings are the available columns in table 'order_attribute':
 * @property string $id
 * @property string $game_id
 * @property string $product_type
 * @property string $name
 * @property string $order_sort
 * @property integer $status
 */
class OrderAttribute extends CActiveRecord {

    public $status_array = array(
        0 => '禁用',
        1 => '启用',
    );

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return OrderAttribute the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'order_attribute';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('game_id, product_type, name', 'required'),
            array('status, game_id, order_sort', 'numerical', 'integerOnly' => true),
            array('product_type', 'length', 'max' => 20),
            array('name', 'length', 'max' => 255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, game_id, product_type, name, order_sort, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'game_id' => '游戏',
            'product_type' => '产品模型',
            'name' => '名称',
            'order_sort' => '排序',
            'status' => '状态',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('game_id', $this->game_id);
        $criteria->compare('product_type', $this->product_type);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'sort' => array(
                        'defaultOrder' => 'game_id ASC',
                    ),
                    'pagination' => array(
                        'pageSize' => 40,
                    )
                ));
    }

    /**
     * 生成输入框
     * @author flwof
     * @param type $name
     * @param type $id
     * @param type $val 
     * @param string $inputType
     */
    public static function createInput($name, $id, $val, $inputType = 'text') {
        switch ($inputType) {
            case 'text':
                if ($name == 'name') {
                    echo CHtml::textField("{$name}_{$id}", $val, array('rel-id' => $id, 'rel-field' => $name, 'onchange' => "updateField(this)"));
                    echo '<a href="#" rel-id="'.$id.'" onclick="copyRow(this)" class="gradient-btn">复制</a>';
                } else {
                    echo CHtml::textField("{$name}_{$id}", $val, array('rel-id' => $id, 'rel-field' => $name, 'onchange' => "updateField(this)"));
                }
                break;
            case 'select':
                switch ($name) {
                    case 'game_id':
                        echo CHtml::dropDownList("{$name}_{$id}", $val, ProductCategory::model()->getParentGame(),array('rel-id' => $id, 'rel-field' => $name, 'onchange' => "updateField(this)"));
                        break;
                    case 'product_type':
                        echo CHtml::dropDownList("{$name}_{$id}", $val, Yii::app()->params->model_array,array('rel-id' => $id, 'rel-field' => $name, 'onchange' => "updateField(this)"));
                        break;
                    case 'status':
                        echo CHtml::dropDownList("{$name}_{$id}", $val, OrderAttribute::model()->status_array,array('rel-id' => $id, 'rel-field' => $name, 'onchange' => "updateField(this)"));
                        break;
                }
                break;
        }
    }

}